package com.maixy.dailydemo.algorithm;

/**
 * @ClassName MyTest1
 * @Description 反转链表
 * 给你单链表的头节点 head ，请你反转链表，并返回反转后的链表。
 * @Date 2021/4/27 13:18
 * @Author maixy
 */
public class MyTest1 {

    public class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) {this.val = val;}
        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
    // 1->2->3->null
    public ListNode reverseList(ListNode head) {
        ListNode prev = null;
        ListNode curr = head;
        while (curr != null) {
            ListNode nextTemp = curr.next;//nextTemp 2->3->null  3->null null
            curr.next = prev;//curr.next null 1->2->3-null 2->3->null
            prev = curr;//prev 1->2->3-null 2->3->null 3->null
            curr = nextTemp;//curr 2->3-null 3->null null
        }
        return prev;
    }
    //3->2->1->null
}
